﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using JeanFish.Blog.BLL;
using JeanFish.Blog.Model;
using JeanFish.Blog.BLL.Relationships;

namespace JeanFish.UnitTest.Blog
{
    [TestClass]
    public class PostTest
    {   
        [TestInitialize]
        public void randomPost()
        {
            postTitle="Post:"+Guid.NewGuid().ToString();
            postText = "Text of " + postTitle;
            blogName = "Blog:" + postTitle;
            postType = "PostType:" + Guid.NewGuid().ToString();
            postCategory = "Category:" + postTitle;
        }
        string postTitle="";
        string postText = "";
        string blogName = "";
        string postType = "";
        string postCategory = "";
        [TestMethod]
        public void BizPostTest()
        {
            BizPost biz = new BizPost();
            int id = 0;
            Post p = biz.GetPost(id);
            Assert.IsNull(p);
            p = biz.CreatePost(postTitle, postText);
            Assert.IsTrue(p.ID > 0);
            Post post2 = biz.GetPost(p.ID);
            Assert.AreEqual(p.ID, post2.ID);
            Assert.AreEqual(p.Title, post2.Title);
            Assert.AreEqual(p.Text, post2.Text);
            string contents="new contents";
            p.Contents = contents;
            p = biz.UpdatePost(p);
            p = biz.GetPost(p.ID);
            Assert.AreEqual(contents, p.Contents);
            p.Delete();
            biz.UpdatePost(p);
            p =biz.GetPost(p.ID);
            Assert.IsNull(p);
            p = biz.CreatePost(postTitle, postText);
            Assert.IsNotNull(p);
        }
        [TestMethod]
        public void BlogPostsTest()
        {
            BlogInfo blog = new BlogInfo(blogName);
            BlogPosts blogPosts = new BlogPosts(blog);
            Assert.IsNotNull(blogPosts);
            Post p1 = new BizPost().CreatePost(postTitle, postText);
            blogPosts.AddValue(p1);
            blogPosts.RemoveValue(p => p.Title == postTitle);
            blogPosts.Save();
            Assert.AreEqual(0, blogPosts.Count);
        }
        [TestMethod]
        [ExpectedException(typeof(BlogException))]
        public void BlogPostsDuplicatedExceptionTest()
        {
            BlogInfo blog = new BizBlogInfo().GetBlogs(null)[0];
            BlogPosts blogPosts = new BlogPosts(blog);
            Assert.IsNotNull(blogPosts);
            Post p1 = new BizPost().CreatePost(postTitle, postText);
            blogPosts.AddValue(p1);
            blogPosts.AddValue(new Post(postTitle, postText));
            blogPosts.Save();
        }
        [TestMethod]
        [ExpectedException(typeof(BlogException))]
        public void PostTypesTest()
        {
            PostTypes types = new PostTypes();
            Meta type= types.Add(postType);
            Assert.IsTrue(type.ID > 0);
            Assert.IsTrue(type== types[postType]);
            types.Add(postType);
        }
        [TestMethod]
        public void PostTypeTest()
        {
            PostTypes postTypes = new PostTypes();
            Meta type=postTypes.Add(postType);
            Assert.IsTrue(type.ID > 0);
            PostType r_PostType = new PostType(new Post( postTitle, postText ));
            r_PostType.AddValue(type);
            r_PostType.Save();
            Assert.AreEqual(1, r_PostType.Count);
        }
        [TestMethod]
        public void TypePostsTest()
        {
            PostTypes types = new PostTypes();
            foreach (var type in types)
            {
                TypePosts tps = new TypePosts(type);
                Assert.IsTrue(tps.Count >= 0);
            }
        }
        [TestMethod]
        public void PostCategoryTest()
        {
            BlogCategory bc = new BlogCategory(new BlogInfo(blogName));
            bc.AddValue(new Category(postCategory));
            bc.AddValue(new Category(postCategory + "1"));
            bc.Save();
            PostCategory pc = new PostCategory(new Post(postTitle, postText));
            pc.AddValue(bc.List());
            pc.RemoveValue(t => t.Title == postCategory);
            pc.Save();
            Assert.AreEqual(1, pc.Count);
        }
        [TestMethod]
        public void PostScenariosTest()
        {
            PostTypes postTypes = new PostTypes();
            Meta type = postTypes.Add(postType);
            PostType r_PostType = new PostType(new Post(postTitle, postText));
            r_PostType.AddValue(type);
            r_PostType.Save();
            Post p = r_PostType.Key;
            BlogPosts b = new BlogPosts(new BlogInfo(blogName));
            b.AddValue(p);
            b.Save();
            PostBlog pb = new PostBlog(p);
            Assert.IsTrue(pb.Count == 1);
        }
    }
}
